package com.chinatsp.controller;

import com.chinatsp.entity.Response;
import com.chinatsp.entity.ResponseUtil;
import com.chinatsp.exception.BusinessException;
import com.chinatsp.service.IService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by chenzhengdong on 2017/3/29.
 */
@RestController
public class NewsController {

    private Logger log = LoggerFactory.getLogger(NewsController.class);

    @Autowired
    private IService iService;

    /**
     * 获取新闻频道
     *
     * @param cnt
     * @param pos
     * @return
     */
    @GetMapping(value = "/api/1.0/news/getchannel/json/")
    public Response newsChannel(String cnt, String pos) throws Exception {
        if (pos == null || pos.isEmpty()) {
            pos = "0";
        }
        if (cnt == null || cnt.isEmpty()) {
            cnt = "10";
        }

        List<Map<String, Object>> listMap = iService.queryNewsChannel(pos, cnt);
        return ResponseUtil.success(listMap);
    }

    /**
     * 获取在线新闻标题
     *
     * @author gaojie @date 2016-8-22
     */
    @RequestMapping(value = "/api/1.0/news/getlist/json/", method = RequestMethod.GET)
    public Response getlist(String channel_id, String pos, String cnt) {
        if (channel_id == null || channel_id.isEmpty()) {
            throw new BusinessException("2001", "channel_id不能为空");
        }
        if (pos == null || pos.isEmpty()) {
            pos = "0";
        }
        if (cnt == null || cnt.isEmpty()) {
            cnt = "10";
        }

        List<Map<String, Object>> listMap = iService.queryNewsList(channel_id, pos, cnt);
        return ResponseUtil.success(listMap);
    }

    /**
     * 获取新闻详细信息
     *
     * @param news_id 新闻id
     * @author gaojie @date 2016-8-22
     */
    @RequestMapping(value = "/api/1.0/news/getinfo/json/", method = RequestMethod.GET)
    public Response getinfo(String news_id) {
        if (news_id == null || news_id.isEmpty()) {
            return ResponseUtil.error("2002", "news_id不能为空");
        }
        List<Map<String, Object>> listMap = iService.getNewsInfo(news_id);
        return ResponseUtil.success(listMap);
    }


}
